package net.csdn.business.common.oauth2.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import net.csdn.business.common.oauth2.model.entity.StatClientAuth;
import net.csdn.business.common.domain.request.oauth.StatClientAuthQuery;
import net.csdn.business.common.domain.vo.oauth.StatClientAuthVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 应用授权统计Mapper接口
 * 
 * @author zhangyal
 * @date 2022-11-16
 */
@Repository
public interface StatClientAuthMapper extends BaseMapper<StatClientAuth>
{

    /**
     * 查询应用授权统计列表
     * 
     * @param query 应用授权统计
     * @return 应用授权统计集合
     */
    @Select("<script> SELECT d.statDate, IFNULL(r.authNums,0) AS authNums " +
            "FROM ( SELECT CURDATE() AS statDate " +
            "UNION ALL " +
            "SELECT DATE_SUB(CURDATE(), INTERVAL 1 DAY) AS statDate " +
            "UNION ALL " +
            "SELECT DATE_SUB(CURDATE(), INTERVAL 2 DAY) AS statDate " +
            "UNION ALL " +
            "SELECT DATE_SUB(CURDATE(), INTERVAL 3 DAY) AS statDate " +
            "UNION ALL " +
            "SELECT DATE_SUB(CURDATE(), INTERVAL 4 DAY) AS statDate " +
            "UNION ALL " +
            "SELECT DATE_SUB(CURDATE(), INTERVAL 5 DAY) AS statDate " +
            "UNION ALL " +
            "SELECT DATE_SUB(CURDATE(), INTERVAL 6 DAY) AS statDate " +
            ") d " +
            "LEFT JOIN ( " +
            "SELECT stat_date as statDate, auth_nums as authNums " +
            "FROM stat_client_auth " +
            "where client_id= #{query.clientId}" +
            ") r ON r.statDate = d.statDate " +
            "GROUP BY d.statDate " +
            "</script>")
    public List<StatClientAuthVO> selectStatClientAuthList(@Param("query") StatClientAuthQuery query);


    /**
     * 修改应用授权统计的数量
     * 
     * @param query 应用授权统计
     * @return 结果
     */
    @Update("update stat_client_auth  set  auth_nums=auth_nums+1 where client_id= #{query.clientId} and stat_date=#{query.statDate}")
    public Integer updateStatClientAuth(@Param("query")StatClientAuthQuery query);


    /**
     * 根据clientId和统计日期查询统计数
     *
     * @param query 应用授权统计
     * @return 结果
     */
    @Select("<script> select id, object_id, auth_nums, client_id, stat_date, created_by, created_date, last_modified_by, last_modified_date from stat_client_auth " +
            "where client_id= #{query.clientId} and stat_date=#{query.statDate}" +
            "</script>")
    public StatClientAuth selectStatClientAuth(@Param("query") StatClientAuthQuery query);

}
